﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace SX.Core
{
    /// <summary>
    /// MD5加密
    /// </summary>
    public class Md5Encrypt
    {
        /// <summary>
        /// 16位MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string Encrypt16(string password)
        {
            if (string.IsNullOrWhiteSpace( password))
                return null;

            using (var md5 = MD5.Create())
            {
                return md5.ComputeHash(Encoding.UTF8.GetBytes(password)).ToHex();
            }
        }

        /// <summary>
        /// 32位MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string Encrypt32(string password = "")
        {
            //x表示16进制 而且区分大小写   X输出为大写   x输出为小写  2表示每次都是两位数
            if (string.IsNullOrWhiteSpace(password))
                return null;
            using (var md5 = MD5.Create())
            {
                string pwd = string.Empty;
                byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
                foreach (var item in s)
                {
                    pwd = string.Concat(pwd, item.ToString("X2"));
                }
                return pwd;
            }
        }

        /// <summary>
        /// 64位MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string Encrypt64(string password)
        {
            if (string.IsNullOrWhiteSpace(password))
                return null;

            using (var md5 = MD5.Create())
            {
                byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(password));           
                return Convert.ToBase64String(s);
            }
        }


    }
}
